"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : show plugins语法验证
Create At   : 2022.08.16
@zou_jialiang0501129488
Description :
    1、创建兼容B库
    1、连接数据库执行show plugins语句查看结果
    1、删除兼容B库
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest02(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_show(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建和node.yml同名且兼容b库的数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists db_show_0061;" \
                  f"create database db_show_0061 dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:执行show plugin语法查看结果;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"show plugins;"
        self.log.info(sql_cmd)
        res = self.sh_primary.execut_db_sql(sql_cmd, dbname='db_show_0061')
        self.log.info(res)
        check_msg = ['Name', 'Status', 'Type',
                     'Library', 'License', 'Comment', 'dolphin']
        for msg in check_msg:
            self.assertIn(msg, res, '插件查询失败')

    def tearDown(self):
        text = '-----step3:删除兼容b库的数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"clean connection to all force for database db_show_0061;" \
                  f"drop database if exists db_show_0061;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end------')
